//
//  NewFeatureViewController.m
//  kuangjia
//
//  Created by 张帅 on 15/10/1.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//


#define  HMNewFeatureCount 4


#import "NewFeatureViewController.h"

#import "WelcomeViewController.h"
@interface NewFeatureViewController ()

@end

@implementation NewFeatureViewController


static NSString* HMNewFeatureCellID= @"HMNewFeatureCellID";

- (instancetype)init
{
    if (self = [super init]) {
     
    }
   
    return [self initWithCollectionViewLayout:[[UICollectionViewFlowLayout alloc]init]];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        
    }
    return self;
}


-(void)viewDidLoad{

    [super  viewDidLoad];
    
    // 注册可重用 cell

    [self prepareLayout];
    [self.collectionView  registerClass:[HMNewFeatureCell class] forCellWithReuseIdentifier:HMNewFeatureCellID];
    
}



- (void)prepareLayout{
    
    
      UICollectionViewFlowLayout* flowlayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
    flowlayout.itemSize = self.view.bounds.size;
    
    flowlayout.minimumLineSpacing =0;
    flowlayout.minimumInteritemSpacing = 0;
 
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    }

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return HMNewFeatureCount;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HMNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HMNewFeatureCellID forIndexPath:indexPath];

    cell.imageIndex = indexPath.item;
    
    return cell;
    
}


- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    // 取当前显示 cell 的 indexPath
    NSIndexPath  *path = collectionView.indexPathsForVisibleItems.lastObject;
    
    if (path.item == HMNewFeatureCount -1) {
        
        HMNewFeatureCell *cell1 =  [collectionView cellForItemAtIndexPath:path];

        [cell1  showStartButton];
    
    }
    
}
    
@end


@interface HMNewFeatureCell()

@property(nonatomic,strong) UIImageView *iconView;


@property(nonatomic,strong) UIButton *startButton;
    


@end

    
    
@implementation HMNewFeatureCell

-(void)setImageIndex:(NSInteger)imageIndex{

 
        _imageIndex = imageIndex;
         NSString *imageName= [NSString stringWithFormat:@"new_feature_%ld",imageIndex +1];
         self.iconView.image =[UIImage imageNamed: imageName];
         self.startButton.hidden = YES;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        
        [self  setUp];
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]){
        [self setUp];
    }
   
    return self;
}


- (void)startButtonClick{
    
 
    NSLog(@"startButtonClick");
    
    [[NSNotificationCenter defaultCenter ] postNotificationName:HMSwitchRootViewControllerNotification object:nil];
    
    
    
}


- (void)setUp {
    
    [self.contentView addSubview:self.iconView];
    [self.contentView  addSubview:self.startButton];
    
    
    self.startButton.translatesAutoresizingMaskIntoConstraints =NO;
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.startButton attribute:(NSLayoutAttributeCenterX) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeCenterX) multiplier:1 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.startButton attribute:(NSLayoutAttributeCenterY) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeBottom) multiplier:1 constant:-160]];
    
    
    
}


-(void) showStartButton{
    
    self.startButton.hidden = NO;
    
    self.startButton.transform =CGAffineTransformMakeScale(0, 0);
 
    [UIView animateWithDuration:0.25 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
       
        self.startButton.transform =CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

- (UIButton *)startButton{
    if (_startButton ==nil) {
        _startButton =[[UIButton alloc] init];
        
        [_startButton setTitle:@"开始" forState:(UIControlStateNormal)];
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:(UIControlStateNormal)];
        [_startButton setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:(UIControlStateHighlighted)];
        
        [_startButton  addTarget:self action:@selector(startButtonClick) forControlEvents:(UIControlEventTouchUpInside)];
        [_startButton sizeToFit];
    }
    return _startButton;
}

- (UIImageView *)iconView{

    if (_iconView==nil) {
    
        _iconView = [[UIImageView alloc] initWithFrame:self.bounds];
    }

    return  _iconView;
}
@end
